package com.clkj.tramcarlibrary.fragment.toTiyuguan;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.clkj.tramcarlibrary.R;
import com.clkj.tramcarlibrary.adapter.JinzhanAdapter;
import com.clkj.tramcarlibrary.adapter.TygShoumoAdapter;
import com.clkj.tramcarlibrary.constant.Constants;
import com.clkj.tramcarlibrary.entity.JinzhanDetail;
import com.clkj.tramcarlibrary.entity.ToTygShoumoDetail;
import com.clkj.tramcarlibrary.http.HttpService;
import com.clkj.tramcarlibrary.schedulers.SchedulerProvider;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class ToTygFragment extends RxFragment implements ToTygContract.View, CompoundButton.OnCheckedChangeListener {

    public String mResult;
    private LinearLayout lyJizhan;
    private LinearLayout lyShoumo;
    private ListView lvJinzhanInfo;
    private ListView lvShoumoInfo;



    private ToggleButton tgState;

    private ToTygContract.Presenter presenter;
    private JinzhanAdapter jinzhanAdapter;
    private TygShoumoAdapter shoumoAdapter;


    List<JinzhanDetail> mJinZhanDetailList = new ArrayList<>();
    List<ToTygShoumoDetail> mShouMoDetailList = new ArrayList<>();

    public static ToTygFragment newInstance(String param) {
        ToTygFragment fragment = new ToTygFragment();
        Bundle bundle = new Bundle();
        bundle.putString("param", param);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mResult = bundle.getString("param");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_to_tyg, container, false);
        presenter = new ToTygPresenter(this, SchedulerProvider.getInstance(), HttpService.Factory.create(Constants.BASE_URL_FOR_TAXI_ABOUT_INFO));

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lyJizhan = (LinearLayout) view.findViewById(R.id.ly_jizhan);
        lyShoumo = (LinearLayout) view.findViewById(R.id.ly_shoumo);
        lvJinzhanInfo = (ListView) view.findViewById(R.id.lv_jinzhan_info);
        lvShoumoInfo = (ListView) view.findViewById(R.id.lv_shoumo_info);
        tgState = (ToggleButton) view.findViewById(R.id.tg_state);

        initView();
    }

    private void initView()
    {


        jinzhanAdapter = new JinzhanAdapter(getActivity(), mJinZhanDetailList);
        lvJinzhanInfo.setAdapter(jinzhanAdapter);


        shoumoAdapter = new TygShoumoAdapter(getActivity(), mShouMoDetailList);
        lvShoumoInfo.setAdapter(shoumoAdapter);

        tgState.setOnCheckedChangeListener(this);

        Observable.interval(0,15*1000, TimeUnit.MILLISECONDS)
                .compose(this.<Long>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        Log.e("timeflys:",aLong/1000+("秒"));
                        presenter.getToTygJinzhan();
                        //getAroundTaxiList(String.valueOf(aMapLocation.getLongitude()),String.valueOf(aMapLocation.getLatitude()));
                    }
                });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            //presenter.getToTygJinzhan();
            lyJizhan.setVisibility(View.VISIBLE);
            lyShoumo.setVisibility(View.GONE);
            lvJinzhanInfo.setVisibility(View.VISIBLE);
            lvShoumoInfo.setVisibility(View.GONE);
        } else {
            lyJizhan.setVisibility(View.GONE);
            lyShoumo.setVisibility(View.VISIBLE);
            lvJinzhanInfo.setVisibility(View.GONE);
            lvShoumoInfo.setVisibility(View.VISIBLE);
            presenter.getToTygShoumo();
        }
    }


    @Override
    public void showToTygJinzhan(List<JinzhanDetail> jinzhanDetails) {

        /*jinzhanAdapter = new JinzhanAdapter(getActivity(), jinzhanDetails);

        lvStationDetails.setAdapter(jinzhanAdapter);*/
        mJinZhanDetailList.clear();
        mJinZhanDetailList.addAll(jinzhanDetails);
        jinzhanAdapter.notifyDataSetChanged();
    }

    @Override
    public void showToTygJinzhanError(String tip) {
        Toast.makeText(getActivity(),tip,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToTygShoumo(List<ToTygShoumoDetail> shoumoDetails) {

        /*shoumoAdapter = new ShoumoAdapter(getActivity(), shoumoDetails);
        lvStationDetails.setAdapter(shoumoAdapter);*/
        mShouMoDetailList.clear();
        mShouMoDetailList.addAll(shoumoDetails);
        shoumoAdapter.notifyDataSetChanged();
    }

    @Override
    public void showToTygShoumoError(String tip) {
        Toast.makeText(getActivity(),tip,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void showError(String tip) {
        Toast.makeText(getActivity(),tip,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(ToTygContract.Presenter presenter) {
        this.presenter = presenter;
    }
}
